"""
Case Type   : 基础功能-兼容B库语法-optimize
Case Name   : optimize列存表
Create At   : 2024/10/29
Owner       : peilinqian
Description :
    step1:创建测试表并插入数据
    step2:创建索引
    step3:删除表中数据，ptimize表
Expect      :
    step1:创建测试表并插入数据 expect:创建插入成功
    step2:创建索引 expect: 创建成功
    step3:删除表中数据，ptimize表 expect:执行正常
History     :
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)}:初始化----')
        self.pri_sh = CommonSH('PrimaryDbBmode')
        self.constant = Constant()
        self.com = Common()
        self.t_name = 't_compatibility_optimize_0011'

    def test_main(self):
        step_txt = '----step1:创建测试表并插入数据 expect: 创建插入成功----'
        self.log.info(step_txt)
        create_sql = f"drop table if exists {self.t_name};" \
                     f"create table {self.t_name}(c1 int,c2 text) " \
                     f"with(orientation=column);" \
                     f"insert into  {self.t_name} values" \
                     f"(generate_series(1,10000),'a');"
        create_result = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(create_result)
        self.assertIn('INSERT', create_result, '执行失败:' + step_txt)

        step_txt = '----step2:创建索引 expect: 创建成功----'
        self.log.info(step_txt)
        create_sql = f"create index test_index_011b on " \
                     f"{self.t_name} using btree(c1);" \
                     f"create index test_index_011g on " \
                     f"{self.t_name} using psort(c2);;"
        create_result = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(create_result)
        self.assertIn(self.constant.CREATE_INDEX_SUCCESS_MSG, create_result,
                      '执行失败:' + step_txt)

        step_txt = '----step3:删除表中数据，ptimize表 expect:执行正常---'
        self.log.info(step_txt)
        sql = f'delete from {self.t_name} where c1>=200 and c1<1000;' \
              f'delete from {self.t_name} where c1>=5000 and c1<9000;' \
              f'optimize verbose local table {self.t_name};'
        optimize_result = self.pri_sh.execut_db_sql(sql)
        self.log.info(optimize_result)
        self.assertIn(f'sample 5200 rows, estimated total 5200 rows',
                      optimize_result, '执行失败:' + step_txt)
        self.assertIn(self.constant.VACUUM_SUCCESS_MSG, optimize_result,
                      '执行失败:' + step_txt)

    def tearDown(self):
        self.log.info('----this is tearDown----')
        step_txt = '----step:清理数据; expect:清理成功----'
        self.log.info(step_txt)
        drop_sql = f"drop table if exists {self.t_name};"
        result = self.pri_sh.execut_db_sql(drop_sql)
        self.log.info(result)

        self.log.info('----teardown断言----')
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, result,
                      "执行失败" + step_txt)

        self.log.info(f'----{os.path.basename(__file__)}:执行完毕----')
